//
//  NewFeatureViewController.swift
//  我的微博
//
//  Created by 李献红 on 15/10/13.
//  Copyright © 2015年 cn.lixianhong. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class NewFeatureViewController: UICollectionViewController {

    /// 图像总数
    private let imageCount = 4
    
    /// 布局属性
    private let layout = BJFlowLayout()
    
    // 没有 override，因为 collectionView 的指定的构造函数是带 layout 的
    init() {
    
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {

        // 本类禁止用 sb 开发 - 纯代码开发，考虑因素少，风险小
        super.init(coder: aDecoder)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }




    // MARK: UICollectionViewDataSource

    // 1. 获取数据源，确认有 cell
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return imageCount
    }

    
     // 3. 获取每个索引对应的cell
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        
        cell.imageIndex = indexPath.item
        cell.startButton.hidden = true
        return cell
    }

    /// 完成显示 cell - indexPath 是之前消失 cell 的 indexPath
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //获取当前显示的 indexPath
        let path = collectionView.indexPathsForVisibleItems().last!
        
        //判断是否是末尾
        if path.item == imageCount - 1 {
        
            //播放动画
            let cell = collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell
            cell.startButtonAnim()
            
            
        }
    }
    
}
    
    
 /// 新特性的cell
class NewFeatureCell: UICollectionViewCell {
    
/// 图像索引 - 私有属性，在同一个文件中，是允许访问的！
    private var imageIndex: Int = 0 {
    
        didSet {
        
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
        }
        
    }
    
    /// 按钮点击事件
    func clickStartButton() {
    
        print("开始")
        NSNotificationCenter.defaultCenter().postNotificationName(BJRootViewControllerSwitchNotification, object: true)
    }
    
    /// 开始按钮动画
    private func startButtonAnim() {
    

        startButton.hidden = false
        
        //把图片缩放至没有
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        //禁用按钮操作
        startButton.userInteractionEnabled = false
        
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            //恢复默认形变(恢复默认尺寸)
            //TODO: 不理解
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                //启用按钮点击
                self.startButton.userInteractionEnabled = true
        }
    }
    
    
    //MARK: - 设置UI
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {

        super.init(coder: aDecoder)
        prepareUI()
    }
    
    private func prepareUI() {
    
        contentView.addSubview(iconView)
        contentView.addSubview(startButton)
        
        
        
        //自动布局
        iconView.ff_Fill(contentView)
        startButton.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: contentView, size: nil, offset: CGPoint(x: 0, y: -160))

        
        
    }
    
    //
    
    //MARK: - 懒加载控件
    private lazy var iconView = UIImageView()
    
    private lazy var startButton: UIButton = {
    
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setTitle("开始体验", forState: UIControlState.Normal)
        
        button.sizeToFit()
        
        button.addTarget(self, action: "clickStartButton", forControlEvents: UIControlEvents.TouchUpInside)
        button.hidden = true
        return button
    }()
}



/// 自定义流水布局
private class BJFlowLayout: UICollectionViewFlowLayout {

    // 2. 如果还没有设置 layout，获取数量之后，准备cell之前，会被调用一次
    // 准备布局属性
    private override func prepareLayout() {
        itemSize = collectionView!.bounds.size
        minimumInteritemSpacing = 0
        minimumLineSpacing = 0
        scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }

}
    

